﻿#if ENABLE_ET_COM
#define ENABLE_AutoItX

namespace suit_etopen
{
    using System;
    using System.Collections.Generic;
    public class CEtOpen : suit_base.SuiteBase
    {
        private autoIt.CAutoIt m_wndKill;
        public override string GetCommand()
        {
            return ET_Improve.Global.CMD_ETOPEN;
        }

        public override void Call()
        {
            try
            {
                String sEtPath = null;
                String sSample = null;
                LoadConfig(sEtPath, sSample);
                etOp.EtOp.ReRegisterET(sEtPath.ToString());
                Dictionary<string, List<tool.CAddInfoHlp.CFileInfo>> fileDic =
                    tool.CAddInfoHlp.GetFolderFiles(sSample.ToString(), ET_Improve.Global.CMD_ETOPEN);
                m_wndKill = new autoIt.CAutoIt();
                m_wndKill.Start();
                foreach (string sDir in fileDic.Keys)
                {
                    tool.LogTool.GetInstance().BeginUseLog(sDir, ET_Improve.Global.s_curCmdName);
                    foreach (tool.CAddInfoHlp.CFileInfo fi in fileDic[sDir])
                    {
                        tool.Output.DumpWriteLine(">>>:" + fi.FileName);
                        ProcessFile(fi);
                    }

                    tool.LogTool.GetInstance().EndUseLog();
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                m_wndKill.Stop();
            }
        }

        protected void LoadConfig(String sEtPath, String sSample)
        {
            const string LOG_ROOT = "etopen";
            const string LOG_ET_PATH = "et-app-path";
            const string LOG_SAMPLE = "sample-path";
            try
            {
                foreach (System.Xml.XmlNode subNode in tool.ConfigReader.GetElementList(LOG_ROOT))
                {
                    switch (subNode.Name)
                    {
                        case LOG_ET_PATH:
                            sEtPath = subNode.InnerText;
                            break;
                        case LOG_SAMPLE:
                            sSample = subNode.InnerText;
                            break;
                    }
                }
            }
            catch (System.Exception ex)
            {
            	 throw ex;               
            }
        }

        protected void ProcessFile(tool.CAddInfoHlp.CFileInfo fi)
        {
            etOp.EtOp pET = new etOp.EtOp();
            try
            {
                m_wndKill.BeginMonitor(true);
                pET.Open(fi.FileName, fi.Password);
                pET.Close();
                m_wndKill.EndMonitor();
            }
            catch (System.Exception ex)
            {
                tool.LogTool.GetInstance().AddLog(tool.LogTool.LOG_TYPE.OPEN_FAILED, null, null, ex);
            }
            finally
            {
                m_wndKill.EndMonitor();
                etOp.EtOp.DestoryET(ref pET);
            }
        }
    }
}
#endif // #if ENABLE_ET_COM